﻿using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace SplitFile
{
	public partial class Form1 : Form
	{
		Thread t;
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (textBox1.Text.Length < 1)
			{
				MessageBox.Show("请输入数字！");
			} else if (textBox2.Text.Length < 1)
			{
				MessageBox.Show("请选择地址！");
			} else
			{
				OpenFileDialog ofd = new OpenFileDialog();

				ofd.Filter = "文件|*.txt";

				if (ofd.ShowDialog() == DialogResult.OK)
				{
					t = new Thread(new ParameterizedThreadStart(Split));
					Control.CheckForIllegalCrossThreadCalls = false;
					t.IsBackground = true;
					t.Priority = ThreadPriority.Highest;
					DATA data = new DATA(ofd.FileName);
					t.Start(data);

				}


			}

		}

		private void button2_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog fbd = new FolderBrowserDialog();
			fbd.Description = "请选择文件夹";
			if (fbd.ShowDialog() == DialogResult.OK)
			{
				if (string.IsNullOrEmpty(fbd.SelectedPath))
				{
					MessageBox.Show(this, "文件夹路径不能为空", "提示");
					return;
				}
				String filePath = fbd.SelectedPath;
				textBox2.Text = filePath;
			}
		}
		private void Split(Object obj)
		{
			MessageBox.Show("开始线程！");

			try
			{
				DATA d = (DATA)obj;
				FileInfo fileInfo = null;
				String FILENAME = d.filename;

				fileInfo = new FileInfo(FILENAME);
				StreamReader sr = new StreamReader(FILENAME, Encoding.UTF8, true);
				//文件大小，以B作为单位
				double fileSize = Math.Ceiling(fileInfo.Length * 1.0);
				Console.WriteLine(fileSize);
				double fileSize2 = 0.0;
				int i = 0;
				int n = Convert.ToInt32(textBox1.Text);
				String[] filePath = FILENAME.Split('\\', '.');
				String fileName = filePath[filePath.Length - 2];
				Console.WriteLine(fileName);
				while (sr.EndOfStream != true)
				{
					fileSize2 = 0;
					String FileName_new = textBox2.Text + "\\" + fileName + "_" + i + ".txt";
					richTextBox1.Text += "FileName_new::" + FileName_new + "\n";
					StreamWriter sw = new StreamWriter(FileName_new, true);
					while (fileSize2 < (fileSize / n))
					{
						String lineContent = sr.ReadLine();
						if (lineContent == null)
							break;
						lineContent = lineContent.Trim();
						//Console.WriteLine(lineContent);
						//richTextBox1.Text += "lineContent::" + lineContent + "\n";
						sw.WriteLine(lineContent);
						fileSize2 += lineContent.Length;
						Console.WriteLine(fileSize2);
						//richTextBox1.Text += "fileSize2::" + fileSize2 + "\n";
					}
					i++;
					//richTextBox1.Text += "i::" + i + "\n";
					if (i > n)
						break;
					sw.Close();
				}
				MessageBox.Show("完成！");
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex);
			}
		}

		private void button3_Click(object sender, EventArgs e)
		{
			t.Abort();
		}

		public class DATA
		{
			public String filename;
			public DATA(String filename)
			{
				this.filename = filename;
			}
		}
	}
}
